package br.com.android.financas.activities;

import java.sql.SQLException;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import br.com.android.financas.R;
import br.com.android.financas.adapter.CategoriaListaAdapter;
import br.com.android.financas.model.Categoria;
import br.com.android.financas.repositorio.RepositorioCategoria;
import br.com.android.financas.repositorio.impl.RepositorioCategoriaImpl;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.ActionMode;

public class ListaCategoria extends SherlockActivity {
	private ActionMode mMode;
	private ListView listaCategorias;
	private Categoria categoria;
	private RepositorioCategoria repositorioCategoria;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setTheme(R.style.Sherlock___Theme_Light);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lista_categoria);

		getSupportActionBar().setSubtitle(R.string.categoria_subtile);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);

		try {
			repositorioCategoria = new RepositorioCategoriaImpl(this);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		listaCategorias = (ListView) findViewById(R.id.list_categorias);

		listaCategorias
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {
					public void onItemClick(AdapterView<?> adapterView,
							View view, int i, long l) {
						Categoria categoria = (Categoria) listaCategorias
								.getAdapter().getItem(i);
						HomeCategoria.callMe(ListaCategoria.this,
								categoria.getId());
					}
				});

		listaCategorias.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View view,
					int i, long l) {
				categoria = (Categoria) listaCategorias.getAdapter().getItem(i);

				mMode = startActionMode(new AnActionModeOfEpicProportions());
                return true;
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		try {
			fillList();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	private void fillList() throws SQLException {
		Log.i(ListaCategoria.class.getName(), "Show list again");

		ArrayAdapter<Categoria> arrayAdapter = new CategoriaListaAdapter(this,
				R.layout.categoria_item, repositorioCategoria.listar());
		listaCategorias.setAdapter(arrayAdapter);
	}

	@Override
	public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
		getSupportMenuInflater().inflate(R.menu.lista_categoria, menu);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		Intent intent;
		switch (item.getItemId()) {
		case android.R.id.home:
			// app icon in action bar clicked; go home
			intent = new Intent(this, Home.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			return true;
		case R.id.menu_add:
			intent = new Intent(this, HomeCategoria.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private final class AnActionModeOfEpicProportions implements
			ActionMode.Callback {

		@Override
		public void onDestroyActionMode(ActionMode mode) {
		}

		@Override
		public boolean onCreateActionMode(ActionMode mode,
				com.actionbarsherlock.view.Menu menu) {
			menu.add("Apagar").setIcon(R.drawable.trash)
					.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

			return true;
		}

		@Override
		public boolean onPrepareActionMode(ActionMode mode,
				com.actionbarsherlock.view.Menu menu) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean onActionItemClicked(ActionMode mode,
				com.actionbarsherlock.view.MenuItem item) {

			try {
				repositorioCategoria.excluir(categoria);
			} catch (SQLException e) {
				e.printStackTrace();
			}

			mode.finish();
			return true;
		}
	}
}
